package controller

import (
	"user-go/entity"

	"github.com/androidsr/sc-go/model"
	"github.com/androidsr/sc-go/sbuilder"
	"github.com/androidsr/sc-go/sgin"
	"github.com/androidsr/sc-go/sno"
	"github.com/androidsr/sc-go/sorm"
	"github.com/gin-gonic/gin"
)

func init() {
	sgin.AddRouter(SysButtonsController{})
}

type SysButtonsController struct {
}

// 查询
// @Router [get] [/sysbuttons/:id]
func (SysButtonsController) Get(c *gin.Context) any {
	id := c.Param("id")
	data := new(entity.SysButtons)
	data.Id = id
	err := sorm.DB.SelectOne(data, data)
	if err != nil {
		return model.NewFailDefault("获取对象失败")
	}
	return data
}

// 增加
// @Router [post] [/sysbuttons]
func (SysButtonsController) Post(c *gin.Context, in *entity.SysButtons) any {
	in.Id = sno.GetString()
	sorm.DB.Insert(in)

	return model.NewOK(nil)
}

// 修改
// @Router [put] [/sysbuttons/edit]
func (SysButtonsController) Put(c *gin.Context, in *entity.SysButtons) any {
	sorm.DB.UpdateById(in)
	return model.NewOK(nil)
}

// 删除
// @Router [delete] [/sysbuttons/:id]
func (SysButtonsController) Delete(c *gin.Context) any {
	id := c.Param("id")
	if id == "" {
		return model.NewOK("主键不能为空")
	}
	sorm.DB.Delete(entity.SysButtons{Id: id})
	return model.NewOK(nil)
}

type SysButtonsQuery struct {
	Page      model.PageInfo    `json:"page"`
	QueryData entity.SysButtons `json:"queryData"`
}

// 查询列表
// @Router [post] [/sysbuttons/page]
func (SysButtonsController) QueryTables(c *gin.Context, in *SysButtonsQuery) any {
	sql := `select * from sys_buttons a where 1=1 `
	data := make([]entity.SysButtons, 0)
	b := sbuilder.StructToBuilder(in.QueryData, sql)
	sql, values := b.Build()
	return sorm.DB.SelectPage(&data, in.Page, sql, values...)
}

// 分页下拉
// @Router [post] [/sysbuttons/list]
func (SysButtonsController) QuerySelect(c *gin.Context, in *model.SelectQueryDTO) any {
	sql := `select a.id as value, a.title as label from sys_buttons a`
	data := make([]model.SelectVO, 0)
	b := sbuilder.Builder(sql)
	b.Like("CONCAT(id,title)", in.Label)
	b.Multiple().Ors(b.In("id", in.Selected))
	sql, values := b.Build()
	return sorm.DB.SelectPage(&data, in.Page, sql, values...)
}
